package com.example.transferfile;

import java.io.File;
import java.io.IOException;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.app.AlertDialog;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class listview extends Activity {

	private List<FTPFile> itemFTP = null;
	private List<String> item = null;
	private List<String> path = null;
	FTPClient ftpClient = new FTPClient();
	private String root;
	//private String rootftp;
	private TextView myPath;
	ListView lv , lv1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_view);
        myPath = (TextView)findViewById(R.id.textView2);
        lv = (ListView)findViewById(R.id.listView1);
        lv1 = (ListView)findViewById(R.id.listView2);
        root = Environment.getExternalStorageDirectory().getPath();
       
        try {
			ftpClient.connect("ftp.byethost33.com",21);
			ftpClient.login("b33_13726508", "123456"); 			
		    ftpClient.enterLocalPassiveMode(); 	
		} catch (SocketException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 			
        getFtp();
        getDir(root);
    }
    private void getFtp(){
    	FTPFile[] ftpFiles;
		try {
			ftpFiles = ftpClient.listFiles();
			itemFTP = new ArrayList<FTPFile>();
					
			for (FTPFile ftpFile : ftpFiles) { 				
//				if (ftpFile.isDirectory()) { 										
//					item.add(ftpFile.getName().toString()+"/"); 			
//				}else{
//					item.add(ftpFile.getName().toString());
//				}
				itemFTP.add(ftpFile);
			} 
			lv1.setAdapter(new adapterfftp(this,itemFTP));
		} catch (IOException e) {
			e.printStackTrace();
		} 
    }
    private void getDir(String dirPath)
    {
    	myPath.setText("Location: " + dirPath);
    	item = new ArrayList<String>();
    	path = new ArrayList<String>();
    	File f = new File(dirPath);
    	File[] files = f.listFiles();
    	
    	if(!dirPath.equals(root))
    	{
    		item.add(root);
    		path.add(root);
    		item.add("../");
    		path.add(f.getParent());	
    	}
    	
    	for(int i=0; i < files.length; i++)
    	{
    		File file = files[i];
    		
    		if(!file.isHidden() && file.canRead()){
    			path.add(file.getPath());
        		if(file.isDirectory()){
        			item.add(file.getName() + "/");
        		}else{
        			item.add(file.getName());
        		}
    		}	
    	}
    	lv.setAdapter(new myadapter(this,item,dirPath));
    	lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				
				onClickItem(position);
			}
		});
    }
    private void onClickItem(int position){
    	File file = new File(path.get(position));
    	
    	if (file.isDirectory())
    	{
    		if(file.canRead()){
    			getDir(path.get(position));
    		}else{
    			new AlertDialog.Builder(this)
    			.setIcon(R.drawable.ic_launcher)
    			.setTitle("[" + file.getName() + "] folder can't be read!")
    			.setPositiveButton("OK", null).show();	
    		}	
    	}else {
    		new AlertDialog.Builder(this)
    		.setIcon(R.drawable.ic_launcher)
    		.setTitle("[" + file.getName() + "]")
    		.setPositiveButton("OK", null).show();
    	  }
    }
}
